package controller;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.filter.HiddenHttpMethodFilter;

import pojo.User;

@Controller
@RequestMapping("/")
public class TestController{
	//没有设置method属性的话，相当于缺省，默认找这个没有method属性的方法
	@RequestMapping(value={"/test1"},method={RequestMethod.POST,RequestMethod.PUT}) //@RequestMapping(value={"/user/*/test1*"})
	public String test01(Model model) throws Exception {
		model.addAttribute("uname","evan");
		return "suc";
	}
	@RequestMapping(value="/test1",method=RequestMethod.GET)
	public String test02(Model model) throws Exception {
		model.addAttribute("uname","nash");
		return "suc";
	}
//	@RequestMapping(value="/test1",headers={"action=load"})
//	@RequestMapping(value="/test1",headers={"Content-Type=application/"})
	@RequestMapping(value="/test1",consumes="application/json",produces="application/json")
	public String test03(Model model) throws Exception {
		model.addAttribute("uname","test03");
		return "suc";
	}
	@RequestMapping(value="/test1",params="id=1")
//	@RequestMapping(value="/test1",params="id!=1")
	public String test04(Model model) throws Exception {
		model.addAttribute("uname","test04");
		return "suc";
	}
	
	
	@RequestMapping(value="/test2")
	public String test04(String uname,int age,@DateTimeFormat(pattern="yyyy-MM-dd")Date birth,Model model) throws Exception {
		System.out.println(birth);
		age++;
		model.addAttribute("uname",uname);
		model.addAttribute("age",age);
		return "suc";
	}
	
	@RequestMapping(value="/test3")
	public String test05(User u,Model model) {
		System.out.println(u.getName()+"\t"+u.getAge()+"\t"+u.getBirth());
		return "suc";
	}
	
	@RequestMapping(value="/test4")
	public String test06(@CookieValue(value="JSESSIONID") String key) {
		System.out.println("key:"+key);
		return "suc";
	}
	@RequestMapping(value="/users/{uid}/books/{bid}")
	public String test07(@PathVariable(value="uid")String uid,@PathVariable int bid) {
		System.out.println("uid:"+uid+"\t"+"bid:"+bid);
		return "suc";
	}
	//Ajax发送json数据并接受json数据
	@ResponseBody
	@RequestMapping(value="test5",consumes="application/json")
	public User test08(@RequestBody User u,Model model ) {
		System.out.println(u.getName()+"\t"+u.getAge()+"\t"+u.getBirth());
		return u;
	}//发送的时候要设置请求头和请求体
	
	//存数据到session  要什么就在参数中添加
	@RequestMapping(value="test6",consumes="application/json")
	public String test08(User u,HttpSession sen,HttpServletRequest request,Model model) {
		sen.setAttribute("xx", "xxx");
		System.out.println(u.getName()+"\t"+u.getAge()+"\t"+u.getBirth());
		return "suc";
	}
	
	//测试拦截器
	@RequestMapping(value="/test002")
	public String test002(){
		System.out.println("execute test2...");
		return "suc";
	}
	
}

